package com.ssm.changeunit.command;

import com.ssm.changeunit.Branch;
import com.ssm.changeunit.BranchFactory;
import com.ssm.changeunit.ChangeUnit;
import com.ssm.changeunit.ChangeUnitException;
import com.ssm.changeunit.Environment;
import com.ssm.changeunit.communicate.ChangeUnitClient;
import com.ssm.changeunit.communicate.ChangeUnitRPC;
import com.ssm.changeunit.communicate.ChangeUnitRPCResult;
import com.ssm.changeunit.persistance.DatabaseManager;
import com.ssm.changeunit.persistance.PersistanceException;
import com.ssm.changeunit.util.ChangeUnitTraveler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 * 
 * @author 시리우스
 */
public class PushCommandWorker extends CommandWorker {
    
    private Logger log = Logger.getLogger(PushCommandWorker.class);
    
    public PushCommandWorker () {
    }
    
    public void work (CommandWorkerOptions commandWorkerOptions) throws CommandWorkerException {
        try {
            String server = "";

            Branch branch = new BranchFactory().createProduct(Environment.getCurrentBranch());

            // 서버에 현재 ChangeUnit을 전송
            ChangeUnitClient changeUnitClient = new ChangeUnitClient(server);

            if( changeUnitClient.isChangeUnitExists(branch.getCurrentChangeUnit().getChangeUnitId(), Environment.getCurrentBranch()) ) 
                throw new CommandWorkerException("Push를 수행할 필요가 없습니다.");
            
            ChangeUnitRPC changeUnitRPC= new ChangeUnitRPC();
            
            List<ChangeUnitRPCResult> changeUnitRPCResultList = new ArrayList<ChangeUnitRPCResult>();
            // 돌면서 생성
            ChangeUnitTraveler changeUnitTraveler = new ChangeUnitTraveler();
            
            changeUnitTraveler.init();
            
            while(changeUnitTraveler.hasNext()) {
                ChangeUnit nextChangeUnit = changeUnitTraveler.getNextChangeUnit();
                
                if( !changeUnitClient.isChangeUnitExists(Environment.getCurrentBranch(), nextChangeUnit.getChangeUnitId()) ) {
                    // 생성
                    changeUnitRPCResultList.add(changeUnitRPC.getChangeUnitRPCResult(server, nextChangeUnit.getChangeUnitId(), nextChangeUnit.getChangeUnitId()));
                }
            }
            
            /*
            // 데이터베이스 체크
            log.debug("데이터베이스가 존재하는지 검사합니다.");

            try {
                if(!DatabaseManager.getInstance().isExists())
                    throw new CommandWorkerException("데이터베이스가 존재하지 않습니다.");
            }
            catch(PersistanceException ex) {
                throw new CommandWorkerException(ex);
            }
             * */
            
            
            ChangeUnitRPCResult changeUnitRPCResult = new ChangeUnitRPCResult();
            for(ChangeUnitRPCResult item : changeUnitRPCResultList){
                changeUnitRPCResult.setChangeLogDTOs(item.getChangeLogDTOs());
                changeUnitRPCResult.setChangeUnitDTOs(item.getChangeUnitDTOs());
                changeUnitRPCResult.setChangeUnitItemsDTOs(item.getChangeUnitItemsDTOs());
                changeUnitRPCResult.setChildDTOs(item.getChildDTOs());
                changeUnitRPCResult.setParentDTOs(item.getParentDTOs());
            }
             
            // 서버에 올림
            changeUnitClient.push(changeUnitRPCResult);
        }
        catch(IOException ex) {
            throw new CommandWorkerException(ex);
        }
        catch(ChangeUnitException ex) {
            throw new CommandWorkerException(ex);
        }
    }
}

